<template>
  <div class="search-wrap">
    <div class="search-input"
         border-radius>
      <input type="text"
             placeholder="城市/拼音"
             class="input-content"
             v-model="keyWord">
    </div>
    <common-scroll class="search-content"
                   ref="wrapper"
                   v-show="keyWord">
      <div>
        <ul class="search-content-list">
          <li class="search-item"
              border-bottom-1px
              v-for="item of list"
              :key="item.id"
              @click="handleChangeCity(item.name)">
            {{item.name}}
          </li>
          <li class="search-item"
              border-bottom-1px
              v-show="hasNoData">没有找到匹配项</li>
        </ul>
      </div>
    </common-scroll>
  </div>
</template>

<script>
import CommonScroll from 'common/scroll/Scroll'
import { mapActions } from 'vuex'
export default {
  name: 'CitySearch',
  components: {
    CommonScroll
  },
  props: {
    cities: {
      type: Object
    }
  },
  data () {
    return {
      timer: null,
      keyWord: '',
      list: [],
      isShow: false
    }
  },
  computed: {
    hasNoData () {
      return !this.list.length
    }
  },
  watch: {
    keyWord () {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        const result = []
        for (let i in this.cities) {
          this.cities[i].map((item) => {
            if (item.pinyin.indexOf(this.keyWord.toLowerCase()) > -1 || item.name.indexOf(this.keyWord) > -1) {
              result.push(item)
            }
          })
        }
        this.list = result
      }, 50)
    }
  },
  methods: {
    handleChangeCity (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapActions({
      changeCity: 'changeCity'
    })
  }
}
</script>

<style lang="stylus" scoped>
@import '~stylus/variable.styl'
@import '~stylus/mixins.styl'

.search-wrap
  padding-left 20px
  padding-right 20px
  padding-top 20px
  padding-bottom 20px
  box-sizing border-box

  .search-input
    width 100%
    height 70px
    box-sizing border-box
    padding-left 40px
    padding-right 40px
    display flex
    align-items center

    .input-content
      font-size $smallFz
      color #757575
      height 66px
      flex 1
      border none
      position relative
      z-index 2

  [border-radius]::before
    border-color #E1E1E1
    border-radius 70px

  .search-content
    position fixed
    top 210px
    left 0
    bottom 0
    right 0
    background-color #fff
    overflow hidden
    z-index 5

    .search-item
      line-height 60px
      font-size $smallFz
      background-color $bgcWh
      text-indent 40px
      cursor pointer
</style>
